/****************************************************************************************
**  File: Color.hpp
**  Author: Asteroth
**  Date: 21/03/2009
****************************************************************************************/

#ifndef __COLOR_HPP__
#define __COLOR_HPP__

template <class T>
class Colorx {
private:
	static T min, max;
	static T null;

public:
	T R, G, B, A;

	Colorx():									R(0), G(0), B(0), A(0) {}
	Colorx(const Colorx<T> &color):				R(color.R), G(color.G), B(color.B), A(color.A) {}
	Colorx(T red, T green, T blue):				R(red), G(green), B(blue), A(0) {}
	Colorx(T red, T green, T blue, T alpha):	R(red), G(green), B(blue), A(alpha) {}

	inline Colorx<T> &operator = (const Colorx<T> &set) {
		R = set.R;
		G = set.G;
		B = set.B;
		A = set.A;
		return *this;
	}

	inline Colorx<T> &operator += (const Colorx<T> &add) {
		R = eMin(R + add.R, max);
		G = eMin(G + add.G, max);
		B = eMin(B + add.B, max);
		A = eMin(A + add.A, max);
		return *this;
	}

	inline Colorx<T> operator + (const Colorx<T> &add) const {
		return Colorx<T>(eMin(R + add.R, max), eMin(G + add.G, max), eMin(B + add.B, max), eMin(A + add.A, max));
	}

	inline Colorx<T> &operator -= (const Colorx<T> &sub) {
		R = eMax(R - sub.R, min);
		G = eMax(G - sub.G, min);
		B = eMax(B - sub.B, min);
		A = eMax(A - sub.A, min);
		return *this;
	}

	inline Colorx<T> operator - (const Colorx<T> &sub) const {
		return Colorx<T>(eMax(R - sub.R, min), eMax(G - sub.G, min), eMax(B - sub.B, min), eMax(A - sub.A, min));
	}

	// Access by index: R=0, G=1, B=2, A=3.
	inline T &operator [] (int index) {
		if (index==0)
			return R;
		else if (index==1)
			return G;
		else if (index==2)
			return B;
		else if (index==3)
			return A;
		return null;
	}
};

template <class T> T Colorx<T>::min = 0;
template <class T> T Colorx<T>::max = sizeof(T)==1? 255: 1;
template <class T> T Colorx<T>::null = 0;

typedef Colorx<ubyte> Color;
typedef Colorx<float> Colorf;

const Color COLOR_BLACK             = Color(0, 0, 0, 255);
const Color COLOR_DARK_GRAY         = Color(63, 63, 63, 255);
const Color COLOR_GRAY              = Color(127, 127, 127, 255);
const Color COLOR_LIGHT_GRAY        = Color(191, 191, 191, 255);
const Color COLOR_WHITE             = Color(255, 255, 255, 255);

const Color COLOR_DARK_RED          = Color(127, 0, 0, 255);
const Color COLOR_DARK_GREEN        = Color(0, 127, 0, 255);
const Color COLOR_DARK_BLUE         = Color(0, 0, 127, 255);
const Color COLOR_DARK_YELLOW       = Color(127, 127, 0, 255);
const Color COLOR_DARK_VIOLET       = Color(127, 0, 127, 255);
const Color COLOR_DARK_CYAN         = Color(0, 127, 127, 255);

const Color COLOR_RED               = Color(255, 0, 0, 255);
const Color COLOR_GREEN             = Color(0, 255, 0, 255);
const Color COLOR_BLUE              = Color(0, 0, 255, 255);
const Color COLOR_YELLOW            = Color(255, 255, 0, 255);
const Color COLOR_VIOLET            = Color(255, 0, 255, 255);
const Color COLOR_CYAN              = Color(0, 255, 255, 255);

const Color COLOR_LIGHT_RED         = Color(255, 127, 127, 255);
const Color COLOR_LIGHT_GREEN       = Color(127, 255, 127, 255);
const Color COLOR_LIGHT_BLUE        = Color(127, 127, 255, 255);
const Color COLOR_LIGHT_YELLOW      = Color(255, 255, 127, 255);
const Color COLOR_LIGHT_VIOLET      = Color(255, 127, 255, 255);
const Color COLOR_LIGHT_CYAN        = Color(127, 255, 255, 255);

#endif
